const mysql = require("mysql");

const pool = mysql.createPool({
    host: '127.0.0.1',
    user: 'root',
    password: '123456',
    database: 'flower', //更改指定的数据库
    port: 3306,
    timeout: 1000 * 5
})


const exec = (sql, params) => {
    return new Promise(function (resolve, reject) {
        //获取链接对象
        pool.getConnection(function (err, conn) {
            if (err) {
                console.log("连接数据库失败!", err.message);
                reject(err.message)
            }
            conn.query(sql, params, (err, result) => {
                if (err) {
                    console.log("执行sql语句失败!", err.message);
                    reject(err.message)
                }
                resolve(result);
                conn.release();
            })
        })
    })
}

module.exports = { exec };